package org.example.collect;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class ArrList {
    private ArrayList<Integer> list;

    public ArrayList<Integer> getList(){
        return list;
    }

    public ArrList(Integer ... elements){
        this.list=new ArrayList<Integer>(5);
        int maxIdx = elements.length-1;
        for(int i=0;i<=maxIdx;i++){
            add(elements[i]);
        }
    }

    public void add(int idx,Integer ele){
        int maxIdx = list.size()-1;
        if(idx>maxIdx){
            list.add(ele);
        }
        else{
            list.add(idx,ele);
        }
    }

/*
//    尾部追加
//
*/

    public void add(Integer ele){
        list.add(ele);
    }

    public void print(boolean isToString){
        if(!isToString){
            System.out.println("元素包括：");
            for(Integer ele:list){
                System.out.print(ele);
                System.out.print("   ");
            }
            System.out.println("\n元素包括：");
            for (int idx = 0; idx <list.size() ; idx++) {
                System.out.print(list.get(idx));
                System.out.print("   ");
            }
            System.out.println("\r\n-----------");
        }
        else{
            System.out.println("元素包括：");
            System.out.print(list.toString());
            System.out.println("\r\n-----------");
        }
    }

    public int max(){
        int maxIdx=list.size()-1;
        int max=list.get(0);

        int targetIdx=-1;
        for (int i = 0; i <=maxIdx ; i++) {
            Integer ele=list.get(i);
            if(ele.compareTo(max)>0){
                targetIdx=i;
                max=ele;
            }
        }
        return max;
    }

    public void removeEle(Integer ele){
        list.remove(ele);
    }

    public void removeByIdx(int idx){
        int maxIdx=list.size()-1;
        if(idx>maxIdx){
            return;
        }
        list.remove(idx);
    }

    public void asc(){
        Collections.sort(list);
    }

    public void desc(){
        Collections.sort(list,Collections.<Integer>reverseOrder());
    }

    /**
     * 排除小于max的元素
     * @param max
     */
    public void exclusive(Integer max){
        List<Integer> res = new ArrayList<>(5);
        Iterator<Integer> iterator=list.iterator();
        while(iterator.hasNext()){
            Integer ele=iterator.next();
            if(ele.compareTo(max)>0){
                iterator.remove();
            }
        }
    }



}
